//
//  NSURL+extend.m
//  mmbang
//
//  Created by 倪元政 on 2016/10/24.
//  Copyright © 2016年 iyaya. All rights reserved.
//

#import "NSURL+extend.h"
#import "NSString+extend.h"

@implementation NSURL (extend)

- (NSDictionary *)queryParams {
    if(!self.query) {
        return  nil;
    }
    
    NSMutableDictionary *ret = [NSMutableDictionary dictionary];
    NSArray *keyValuePairs = [self.query componentsSeparatedByString:@"&"];
    for(id kv in keyValuePairs) {
        NSArray *kvPair = [kv componentsSeparatedByString:@"="];
        if(kvPair.count > 1)
        {
            NSString *key = [kvPair objectAtIndex:0];
            NSString *value = [kvPair objectAtIndex:1];
            
            NSString *origStr = [value stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            if (!origStr) {
                continue;
            }
            [ret setValue:origStr forKey:key];
        }
    }
    
    return ret;
}

- (NSURL *)addParams:(NSDictionary *)params {
    NSMutableString *_add = nil;
    if(NSNotFound != [self.absoluteString rangeOfString:@"#"].location) {
        NSArray *array = [self.absoluteString componentsSeparatedByString:@"#"];
        if(array.count > 1) {
            NSString *header = [array objectAtIndex:0];
            if (NSNotFound != [header rangeOfString:@"?"].location) {
                _add = [NSMutableString stringWithString:@"&"];
            } else {
                _add = [NSMutableString stringWithString:@"?"];
            }
            for (NSString* key in [params allKeys]) {
                if ([params objectForKey:key] && 0 < [[params objectForKey:key] length]) {
                    [_add appendFormat:@"%@=%@&",key,[[params objectForKey:key] urlEncodedString]];
                }
            }
            NSString *footer = [array objectAtIndex:1];
            
            return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@#%@",
                                         NONILString(header),
                                         [_add substringToIndex:[_add length] - 1],
                                         NONILString(footer)]];
        }
    } else {
        if (NSNotFound != [self.absoluteString rangeOfString:@"?"].location) {
            _add = [NSMutableString stringWithString:@"&"];
        } else {
            _add = [NSMutableString stringWithString:@"?"];
        }
        for (NSString* key in [params allKeys]) {
            if ([params objectForKey:key] && 0 < [[params objectForKey:key] length]) {
                [_add appendFormat:@"%@=%@&",key,[[params objectForKey:key] urlEncodedString]];
                
            }
        }
        
        return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",
                                     self.absoluteString,
                                     [_add substringToIndex:[_add length] - 1]]];
    }
    return self;
}

- (NSString *)trimTailZerosAfterDotOfString:(NSString *)string {
    NSInteger loc = string.length - 1;
    NSString *curent = @"";
    while (0 <= loc) {
        curent = [string substringFromIndex:loc];
        if ([@"." isEqualToString:curent]) {
            loc ++;
            break;
        }
        if (! [@"0" isEqualToString:curent] && 0 < [curent integerValue]) {
            loc ++;
            break;
        }
        loc --;
    }
    return [string substringToIndex:loc];
}

- (NSString *)stringFromValue:(id)value {
    NSString *ret;
    if([value isKindOfClass:[NSNumber class]]) {
        if (0 == strcmp([(NSNumber *)value objCType], @encode(double))) {
            ret = [self trimTailZerosAfterDotOfString:[NSString stringWithFormat:@"%lf", [(NSNumber *)value doubleValue]]];
        } else if (0 == strcmp([(NSNumber *)value objCType], @encode(float))) {
            ret = [self trimTailZerosAfterDotOfString:[NSString stringWithFormat:@"%f", [(NSNumber *)value floatValue]]];
        } else if (0 == strcmp([(NSNumber *)value objCType], @encode(int))) {
            ret = [NSString stringWithFormat:@"%d", [(NSNumber *)value intValue]];
        } else if (0 == strcmp([(NSNumber *)value objCType], @encode(BOOL))) {
            ret = [NSString stringWithFormat:@"%d", [(NSNumber *)value boolValue]];
        } else if (0 == strcmp([(NSNumber *)value objCType], @encode(long))) {
            ret = [NSString stringWithFormat:@"%ld", [(NSNumber *)value longValue]];
        } else if (0 == strcmp([(NSNumber *)value objCType], @encode(long long))) {
            ret = [NSString stringWithFormat:@"%lld", [(NSNumber *)value longLongValue]];
        } else {
            ret = [NSString stringWithFormat:@"%.2f", [(NSNumber *)value floatValue]];
        }
    } else {
        ret = value;
    }
    return ret;
}

- (NSURL *)appendParams:(NSDictionary *)params {
    NSMutableString *_add = nil;
    if(NSNotFound != [self.absoluteString rangeOfString:@"#"].location) {
        NSArray *array = [self.absoluteString componentsSeparatedByString:@"#"];
        if(array.count > 1) {
            NSString *header = [array objectAtIndex:0];
            if (NSNotFound != [header rangeOfString:@"?"].location) {
                _add = [NSMutableString stringWithString:@"&"];
            } else {
                _add = [NSMutableString stringWithString:@"?"];
            }
            for (NSString* key in [params allKeys]) {
                NSString *value = [self stringFromValue:[params objectForKey:key]];
                if (value) {
                    [_add appendFormat:@"%@=%@&",key,[value urlEncodedString]];
                }
            }
            NSString *footer = [array objectAtIndex:1];
            
            return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@#%@",
                                         NONILString(header),
                                         [_add substringToIndex:[_add length] - 1],
                                         NONILString(footer)]];
        }
    } else {
        if (NSNotFound != [self.absoluteString rangeOfString:@"?"].location) {
            _add = [NSMutableString stringWithString:@"&"];
        } else {
            _add = [NSMutableString stringWithString:@"?"];
        }
        for (NSString* key in [params allKeys]) {
            NSString *value = [self stringFromValue:[params objectForKey:key]];
            if (value) {
                [_add appendFormat:@"%@=%@&",key,[value urlEncodedString]];
            }
        }
        return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",
                                     self.absoluteString,
                                     [_add substringToIndex:[_add length] - 1]]];
    }
    return self;
}



@end
